package com.ld.rightsSystem.mapper;

import com.ld.rightsSystem.pojo.TPermission;
import org.hibernate.annotations.SQLInsert;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单权限表 Mapper 接口
 * </p>
 *
 * @author lhb
 * @since 2023-12-27
 */
@Repository
public interface TPermissionRepository extends JpaRepository<TPermission,Long> {

    @Transactional
    @Modifying
    @Query(value = "insert into auth.t_permission (permission_name,parent_id,permission_order,component,permission_code,permission_icon,create_by,remark) " +
            "values (:#{#tPermission.permissionName},:#{#tPermission.parentId},:#{#tPermission.permissionOrder}," +
            ":#{#tPermission.component},:#{#tPermission.permissionCode},:#{#tPermission.permissionIcon},:#{#tPermission.createBy},:#{#tPermission.remark})",nativeQuery = true)
    int insert(@Param("tPermission") TPermission tPermission);

    @Transactional
    @Modifying
    @Query(value = "update auth.t_permission set permission_name=:#{#tPermission.permissionName}," +
            "parent_id=:#{#tPermission.parentId},permission_order=:#{#tPermission.permissionOrder}," +
            "component=:#{#tPermission.component},permission_code=:#{#tPermission.permissionCode}," +
            "permission_icon=:#{#tPermission.permissionIcon},update_by=:#{#tPermission.updateBy}," +
            "update_time=:#{#tPermission.updateTime},remark=:#{#tPermission.remark} " +
            "where permission_id=:#{#tPermission.permissionId}",nativeQuery = true)
    int updateTPermission(@Param("tPermission") TPermission tPermission);

    @Query(value = "select * from auth.t_permission where del_status=0 and permission_id=:permissionId",nativeQuery = true)
    TPermission findTPermissionByPermissionId(@Param("permissionId") Long permissionId);

    @Transactional
    @Modifying
    @Query(value = "update auth.t_permission set del_status=1,update_by=:updateBy,update_time=:updateTime where permission_id=:permissionId",nativeQuery = true)
    int deleteTPermissionByPermissionId(@Param("permissionId")Long permissionId, @Param("updateBy") String updateBy, @Param("updateTime") Date updateTime);


    @Query(value = "select * from auth.t_permission where del_status=0",nativeQuery = true)
    List<TPermission> findTPermissionsPageable(PageRequest pageRequest);


    @Query(value = "select permission_id from auth.t_permission where del_status=0 and permission_name=:permissionName",nativeQuery = true)
    Long findTPermissionIdByPermissionName(@Param("permissionName") String permissionName);

    @Query(value = "select permission_id from auth.t_permission where del_status=0 and permission_code=:permissionCode",nativeQuery = true)
    Long findTPermissionIdByPermissionCode(@Param("permissionCode") String permissionCode);
}
